home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DBio / DSeqViews.cpp < prev    next >
Text File  |  1996-07-05  |  49KB  |  2,069 lines

  1. // DSeqViews.cpp
  2.  
  3. #define MASKS 1
  4.  
  5. #include "DSeqViews.h"
  6.  
  7. #include <ncbi.h>
  8. #include <dgg.h>
  9. #include <Dvibrant.h>
  10. #include <DApplication.h>
  11. #include <DClipboard.h>
  12. #include <DControl.h>
  13. #include <DWindow.h>
  14. #include <DRichViewNu.h>
  15. #include <DTableView.h>
  16. #include <DViewCentral.h>
  17. #include <DTask.h>
  18. #include <DTracker.h>
  19. #include <DMenu.h>
  20. #include <DUtil.h>
  21. #include <DFindDlog.h>
  22. #include "DSeqFile.h"
  23. #include "DSeqEd.h"
  24. #include "DSeqMail.h"
  25. #include "DSeqCmds.h"
  26. #include "DSeqChildApp.h"
  27. #include "DSeqPrint.h"
  28. #include "DSeqPict.h"
  29. #include "DSeqDoc.h"
  30.  
  31.  
  32.  
  33. #define BACKCOLOR
  34. #define TESTVARHEIGHT
  35. #undef TESTZOOM
  36.  
  37.  
  38. Local short gAlnCharWidth = 12;
  39. Local short gAlnCharHeight = 15;
  40. Local short kORFxtraHeight = 4; // also in DSeqAsmView.cpp !
  41.  
  42. //Local short gMinCommonPercent = 0;
  43. Local Boolean gSwapBackground = false;
  44. typedef unsigned long  colorVal;
  45.  
  46. extern "C" void Nlm_SelectBackColor(Nlm_Uint1 red, Nlm_Uint1 green, Nlm_Uint1 blue);
  47. extern "C" void Nlm_SetBackColor (Nlm_Uint4 color);
  48.  
  49.  
  50. #define ETEXT 1
  51.  
  52.  
  53. extern "C" void Nlm_HScrollText (Nlm_BaR sb, Nlm_GraphiC t,
  54.                                Nlm_Int2 newval, Nlm_Int2 oldval);
  55. extern "C" void Nlm_DoActivate(void* g, Nlm_Boolean saveport);
  56. //extern Nlm_Boolean   gDialogTextMultiline;
  57. extern "C" void Nlm_SetTextColumns( Nlm_GraphiC t, Nlm_Int2 ncolumns);
  58.  
  59.  
  60.  
  61. // class DAlnSequence
  62.  
  63. #ifdef  WIN_MAC
  64. #define kTextLineCharwidth    1
  65. #else
  66. #define kTextLineCharwidth    charwidth
  67. #endif
  68.  
  69. DAlnSequence::DAlnSequence(long id, DAlnView* itsSuperior, short charwidth):
  70.         DTextLine(id, itsSuperior->fDoc, NULL, kTextLineCharwidth, gSeqFont),  
  71.         fSeq(NULL),fVisible(false), fNcols(0)
  72. {
  73.     fAlnView= itsSuperior;
  74.     SetMultilineText(false);
  75. }
  76.  
  77. void DAlnSequence::Scroll(Boolean vertical, DView* scrollee, long newval, long oldval) 
  78. {
  79.     if (fVisible && !vertical) { 
  80. #ifndef WIN_MAC
  81.     long curlen= Nlm_TextLength(fText);
  82.     if (newval > curlen) newval= curlen;
  83. #endif
  84.         Nlm_HScrollText( NULL, (Nlm_GraphiC)GetNlmObject(), newval, oldval);
  85.         }
  86. }
  87.  
  88. void DAlnSequence::SetColumns( long ncolumns)
  89. {
  90.     if (fNcols != ncolumns) {
  91.         fNcols= ncolumns;
  92.         Nlm_SetTextColumns( (Nlm_GraphiC)GetNlmObject(), ncolumns);
  93.         }
  94. }
  95.  
  96. void DAlnSequence::Select(long start, long length)
  97. {
  98.     Nlm_SelectText(fText, start, length + start);
  99. }
  100.  
  101. Boolean DAlnSequence::IsMyAction(DTaskMaster* action) 
  102. {
  103.     switch(action->Id()) {
  104.         case DApplication::kFind:
  105.         case DApplication::kFindAgain:
  106.             return gApplication->DoMenuTask(action->Id(), NULL);
  107.         case DSeqDoc::cFindORF:
  108.             return fAlnView->fDoc->DoMenuTask( action->Id(), NULL);
  109.         default:
  110.             return DDialogText::IsMyAction(action);
  111.         }
  112. }
  113.  
  114.  
  115.  
  116. void DAlnSequence::ShowEdit(Boolean newseq)
  117. {
  118.     if (newseq && fSeq) {
  119.         SetText( fSeq->Bases());
  120.         }
  121.     if (!fVisible) {
  122.         fVisible= true;
  123.         Show(); 
  124.         Enable();
  125.         }
  126.         // may need this for MSWIN
  127.     //Nlm_DoActivate(GetNlmObject(),false); 
  128. }
  129.  
  130. void DAlnSequence::HideEdit()
  131. {
  132.     if (fVisible) {
  133.         fVisible= false;
  134.         Disable();
  135.         Hide();
  136.         }
  137. }
  138.  
  139. void DAlnSequence::selectAction()
  140. {
  141.     DTextLine::selectAction();
  142.     //gCursor->ibeam();
  143.     SetKeyHandler(this); //?? also need this call in a window activate/deactivate method??
  144. }
  145.  
  146. void DAlnSequence::deselectAction()
  147. {
  148.     SetKeyHandler(NULL); //?? also need this call in a window activate/deactivate method??
  149.     //fDoc->SetEditText(NULL);
  150.     //gCursor->arrow();
  151.     DTextLine::deselectAction();
  152. }
  153.  
  154. void DAlnSequence::ProcessKey( char c)
  155. {
  156.     enum moves {
  157.         down1 =  1,
  158.         up1        = -1,
  159.         downPage= 10,  // this is arbitrary...
  160.         upPage  = -10,
  161.         toBottom=  25000,
  162.         toTop        = -25000
  163.         };
  164.         
  165.     if ( IsEnabled() && !gKeys->command()) { 
  166.         // dang vibrant doens't have means to change this key, and default handlers still get
  167.         // a crack at it...
  168.  
  169.         switch (c) {
  170.         
  171. #if 0
  172.             case chTab: 
  173.               //? do horizontal shift here ?
  174.                 break;
  175.  
  176.             case chRight:
  177.                 if (gKeys->shift()) ;
  178.                 else ;
  179.             case chLeft:
  180.                 if (gKeys->shift()) ;
  181.                 else ;
  182.                 
  183.             case chUp:
  184.                 if (gKeys->shift()) vertMove(toTop);
  185.                 else vertMove(up1);
  186.                 break;
  187.                 
  188.             case chDown:
  189.             case chEnter:
  190.             case chReturn:
  191.                 if (gKeys->shift())  vertMove(toBottom);
  192.                 else vertMove(down1);
  193.                 break;
  194.                 
  195.             case chPageDown    :    vertMove( downPage); break;
  196.             case chPageUp        :    vertMove( upPage); break;
  197.             case chHome            : vertMove( toTop); break;
  198.             case chEnd             : vertMove( toBottom); break;    
  199. #endif
  200.         
  201.             default: 
  202.                 if (!fSeq->GoodChar(c)) Nlm_Beep(); 
  203.                 break; 
  204.             }
  205.         }
  206. }
  207.  
  208.  
  209.  
  210. class DMaskSelector : public DTabSelector 
  211. {
  212. public:
  213.     enum { cMaskSelector = 20732 };
  214.     short fMaskLevel;
  215.     DSeqList* fSeqList;
  216.     
  217.     DMaskSelector( DTableView* itsTable, short masklevel, DSeqList* seqlist) : 
  218.         DTabSelector( itsTable),
  219.         fSeqList( seqlist),  fMaskLevel(masklevel) 
  220.         {
  221.         fNumber= cMaskSelector;
  222.         }
  223.     virtual void DoItWork(); 
  224. };
  225.  
  226.  
  227. void DMaskSelector::DoItWork()  
  228. {
  229.     if (fMovedOnce || fDoExtend) {
  230. #if MASKS
  231.             // select masks... instead of, or as well as, table sel rect??
  232.         if (fMaskLevel>0) {
  233.             short row, col, top, bot, left, right;
  234.             Nlm_RecT viewr;
  235.             top= fNewSelection.top;
  236.             bot= fNewSelection.bottom;
  237.             left= fNewSelection.left;
  238.             right= fNewSelection.right;
  239.             for (row= top; row<bot; row++) {
  240.                 DSequence* sq= fSeqList->SeqAt(row);
  241.                 if (sq) {
  242.                     for (col= left; col<right; col++) 
  243.                       if (fDoExtend) sq->SetMaskAt(col,fMaskLevel);
  244.                         else sq->FlipMaskAt(col, fMaskLevel);
  245.                     }                
  246.                 }
  247.             ((DTableView*)fView)->GetCellRect(fNewSelection,viewr);
  248.             ((DTableView*)fView)->InvalRect( viewr);
  249.             }
  250.         else
  251. #endif
  252.         ((DTableView*)fView)->SelectCells( fNewSelection, 
  253.                 fDoExtend,  DTabSelection::kHighlight,  DTabSelection::kSelect);
  254.         }
  255. }
  256.  
  257.  
  258.  
  259.  
  260. static short gBaseCharWidth = 12;
  261.  
  262. //inline 
  263. static short BaseCharWidth()
  264. {
  265. #ifdef TESTZOOM
  266.     if (gBaseCharWidth>1) gBaseCharWidth= Nlm_CharWidth('G');
  267. #else
  268.     gBaseCharWidth= Nlm_CharWidth('G');
  269. #endif
  270.     return gBaseCharWidth;
  271.     //return Nlm_MaxCharWidth(); 
  272.     //return Nlm_stdCharWidth;
  273. }
  274.  
  275.  
  276.  
  277.  
  278. //class DAlnView : public DTableView  
  279.  
  280.     // test var for fEditSeq bomb...
  281. DAlnView* gLastEditView = NULL;
  282.  
  283.  
  284. DAlnView::DAlnView( long id, DView* itsSuper, DSeqDoc* itsDocument, DSeqList* itsSeqList, 
  285.                                 long pixwidth, long pixheight) :
  286.     DTableView( id, itsSuper, pixwidth, pixheight, 0, 0, Nlm_stdCharWidth, 2+Nlm_stdFontHeight,
  287.                         true, true),
  288.     fDoc(itsDocument), fSeqList(itsSeqList), fSlider(NULL),
  289.     fEditRow(-1), fMaskLevel(0), fCurSeq(NULL), fEditSeq(NULL),
  290.     fLocked(false), fOwnSeqlist(false), 
  291.     fColorBases(kBaseBlack), fColcolors(NULL), fNcolcolors(0)
  292.     fKind= kindAlnView;
  293. #ifdef TESTZOOM
  294.     if (gKeys->shift()) gBaseCharWidth= 1; else gBaseCharWidth= Nlm_stdCharWidth;
  295. #endif
  296.     gAlnCharWidth= gBaseCharWidth; //Nlm_stdCharWidth; //BaseCharWidth();   
  297.     gAlnCharHeight= Nlm_stdLineHeight;
  298.     this->SetResize( DView::relsuper, DView::relsuper);
  299.     this->SetTableFont(gSeqFont);
  300.     //fSlider= new DAlnSlider( fDoc, this, 0);
  301. }
  302.  
  303.  
  304. enum { 
  305.      kAlnIndexName = 0,
  306.      kAlnIndexSize,
  307.      kAlnIndexKind,
  308.      kAlnIndexRow,
  309.      kAlnIndexChecksum,
  310.      kAlnIndexCols,            // end of listings
  311.      kAlnIndexShowORF,     // hide this one for now
  312.      knada
  313.      };
  314.  
  315. DAlnIndex::DAlnIndex( long id, DView* itsSuper, DSeqDoc* itsDocument, DSeqList* itsSeqList,
  316.                  long pixwidth, long pixheight) :
  317.     DTableView( id, itsSuper, pixwidth, pixheight, 0, kAlnIndexCols,
  318.             5*Nlm_stdCharWidth, 2+Nlm_stdFontHeight, false, true),
  319.     fNameWidth(15),
  320.     fSizeWidth(8),
  321.     fKindWidth(8),
  322.     fDoc(itsDocument),
  323.     fSeqList(itsSeqList)
  324.     //this->SetSlateBorder(false);
  325.     this->SetResize( DView::fixed, DView::relsuper);
  326.     this->SetTableFont(gTextFont);
  327.     //fChangedFont= gItalicTextFont;
  328. }
  329.  
  330. DAlnITitle::DAlnITitle( long id, DView* itsSuper, long pixwidth, long pixheight) :
  331.     DTableView( id, itsSuper, pixwidth, pixheight, 1, kAlnIndexCols,
  332.             5*Nlm_stdCharWidth, 2+Nlm_stdFontHeight, false, false),
  333.     fNameWidth(15),
  334.     fSizeWidth(8),
  335.     fKindWidth(8)
  336.     //this->SetSlateBorder(false);
  337.     this->SetResize( DView::fixed, DView::fixed);
  338.     this->SetTableFont(gTextFont);
  339. }
  340.  
  341. DAlnHIndex::DAlnHIndex( long id, DView* itsSuper, DSeqDoc* itsDocument, DSeqList* itsSeqList, long pixwidth, long pixheight) :
  342.     DPanel( id, itsSuper, pixwidth, pixheight, DPanel::simple),
  343.     fDoc(itsDocument), fLastCol(-1),
  344.     fSeqList(itsSeqList)
  345.     //this->SetResize( DView::matchsuper, DView::fixed);
  346.     this->SetResize( DView::relsuper, DView::fixed);
  347. }
  348.  
  349. DAlnView::~DAlnView()
  350. {
  351.     //fSeqList= NULL; // fSeqList is owned by Doc !
  352.     DeInstallEditSeq();
  353.     //if (fSlider) delete fSlider;
  354. #if FIX_LATER
  355.     itsEditSeq= fEditSeq;     
  356.     if (itsEditSeq && itsEditSeq->fSuperior)
  357.         itsEditSeq->fSuperior->RemoveSubView(itsEditSeq);
  358.     delete itsEditSeq;
  359. #endif
  360. }
  361.  
  362.                 
  363.  
  364. void DAlnHIndex::Resize(DView* superview, Nlm_PoinT sizechange)
  365. {
  366.     DPanel::Resize(superview, sizechange);
  367. }
  368.  
  369. void DAlnView::Resize(DView* superview, Nlm_PoinT sizechange)
  370. {
  371.     DTableView::Resize(superview, sizechange);
  372.     CheckViewCharWidth();
  373. }
  374.  
  375. void DAlnView::CheckViewCharWidth()
  376. {
  377.     if (fEditSeq) {
  378.         Nlm_RecT vr;
  379.         ViewRect( vr);
  380.         long cwidth= (vr.right - vr.left) / GetItemWidth(0);
  381.         fEditSeq->SetColumns( cwidth);
  382.         }
  383. }
  384.  
  385.  
  386. void DAlnView::GetReadyToShow()
  387. {
  388.     // assume port is set??
  389.     SelectFont();  
  390.     gAlnCharWidth = BaseCharWidth();   
  391.         // use Nlm_FontHeight for background color to adjoin among rows
  392.     gAlnCharHeight= Nlm_FontHeight(); //Nlm_LineHeight(); // + 2; 
  393.     SetItemWidth(0, GetMaxCols(), gAlnCharWidth);
  394.     
  395. #ifdef notnow_TESTVARHEIGHT
  396.     // testing
  397.     long i, nrow= GetMaxRows();
  398.     for (i= 0; i<nrow; i += 2) {
  399.         short ht= gAlnCharHeight;
  400.         if ( i % 4 == 2) ht *= 2;
  401.         SetItemHeight(i, 2, ht);
  402.         }    
  403. #else
  404.     SetItemHeight(0, GetMaxRows(), gAlnCharHeight);
  405. #endif
  406.     this->UpdateAllWidths(); 
  407.     this->UpdateSize();  
  408. }
  409.  
  410. void DAlnIndex::GetReadyToShow()
  411. {
  412.     //SelectFont();  
  413.     SetItemWidth(0, GetMaxCols(), fViewrect.right-fViewrect.left-1); //<< width of view
  414. #ifdef TESTVARHEIGHT
  415.     // testing
  416.     DAlnView* aview= fDoc->fAlnView;
  417.     long i, nrow= GetMaxRows();
  418.     for (i= 0; i<nrow; i++) {
  419.         long ht= aview->GetItemHeight(i);
  420.         SetItemHeight(i, 1, ht);
  421.         }
  422. #else
  423.     SetItemHeight(0, GetMaxRows(), gAlnCharHeight);
  424. #endif
  425. }
  426.  
  427. void DAlnITitle::GetReadyToShow()
  428. {
  429.     // assume port is set??
  430.     SelectFont();
  431.     short charheight=  Nlm_LineHeight(); // Nlm_FontHeight();
  432.     SetItemWidth(0, GetMaxCols(), fViewrect.right-fViewrect.left-1); //<< width of view
  433.     SetItemHeight(0, GetMaxRows(), charheight);
  434. }
  435.  
  436. void DAlnView::Show()
  437. {
  438.     GetReadyToShow();
  439.     DTableView::Show();
  440. }
  441.  
  442. void DAlnIndex::Show()
  443. {
  444.     GetReadyToShow();
  445.     DTableView::Show();
  446. }
  447.  
  448. void DAlnView::Drag(Nlm_PoinT mouse)
  449. {
  450. #if 0
  451.     short     row, col;
  452.     PointToCell( mouse, row, col);
  453.     SeqMeter(row,col);
  454. #endif
  455.     DTableView::Drag(mouse); // sets fMouseStillDown= true
  456. }
  457.  
  458. void DAlnIndex::Drag(Nlm_PoinT mouse)
  459. {
  460.     DTableView::Drag(mouse); // sets fMouseStillDown= true
  461. }
  462.  
  463. void DAlnHIndex::Drag(Nlm_PoinT mouse)
  464. {
  465.     DPanel::Drag(mouse);
  466.     ClickColumn(mouse);
  467. }
  468.  
  469. void DAlnView::Hold(Nlm_PoinT mouse)
  470. {
  471.     DTableView::Hold(mouse);
  472. }
  473.  
  474. void DAlnIndex::Hold(Nlm_PoinT mouse)
  475. {
  476.     DTableView::Hold(mouse);
  477. }
  478.  
  479. void DAlnHIndex::Hold(Nlm_PoinT mouse)
  480. {
  481.     DPanel::Hold(mouse);
  482. }
  483.  
  484. void DAlnView::Release(Nlm_PoinT mouse)
  485. {
  486.     DTableView::Release(mouse);
  487. }
  488.  
  489. void DAlnIndex::Release(Nlm_PoinT mouse)
  490. {
  491.     DTableView::Release(mouse);
  492. }
  493.  
  494. void DAlnHIndex::Release(Nlm_PoinT mouse)
  495. {
  496.     DPanel::Release(mouse);
  497. }
  498.  
  499.  
  500. void DAlnView::DoubleClickAt(long row, long col)
  501. {
  502. #if 0
  503.     //?? do this only on DAlnIndex dblclik??
  504.     DSequence* ag= fSeqList->SeqAt(row); 
  505.     if (ag) fDoc->OpenSeqedWindow(ag);
  506. #endif
  507. }
  508.  
  509.  
  510. void DAlnIndex::Click(Nlm_PoinT mouse)
  511. {
  512.     long      row, col;
  513.     //if (gLastCommand) { delete gLastCommand; gLastCommand= NULL; } //gLastCommand->Commit();    //??
  514.     
  515.     fDoc->fAlnView->DeInstallEditSeq();
  516.     fDoc->fAlnView->SetEmptySelection(true); 
  517.  
  518.     PointToCell( mouse, row, col);
  519.     
  520.     if (col == kAlnIndexShowORF) {
  521.         DSequence* aSeq= fDoc->fSeqList->SeqAt(row);
  522.         if (aSeq) {
  523.             if (aSeq->ShowORF()) aSeq->SetShowORF(0);  
  524.             else aSeq->SetShowORF(1+2+4+8);
  525.             fDoc->fAlnView->Invalidate();  
  526.             this->Invalidate();
  527.             fDoc->fAlnView->GetReadyToShow(); 
  528.             this->GetReadyToShow();
  529.             }
  530.         return;
  531.         }
  532.         
  533.     else if (Nlm_dblClick)
  534.         ;
  535.     else if (IsSelected(row, col)) {   //if (gKeys->shift()) 
  536.         // slide/shift selected lines...
  537.         DAlnShifter* shifter= new DAlnShifter();  
  538.         shifter->IAlnSlider( fDoc, this, fDoc->fAlnView, row);
  539.         fCurrentTracker= shifter;
  540.         }
  541.     else {
  542.         //if (fTabSelector) fTabSelector->suicide();  // is this delete causing bombs? YES, in motif
  543.         fTabSelector= new DTabSelector( this);
  544.         fCurrentTracker= fTabSelector;
  545.      }
  546.     DTableView::Click( mouse);
  547. }
  548.  
  549.  
  550. void DAlnITitle::Click(Nlm_PoinT mouse)
  551. {
  552.         // damn, need this or the like for each user of DTableView::Click ...
  553.     //if (gLastCommand) { delete gLastCommand; gLastCommand= NULL; } //gLastCommand->Commit();    //??
  554.  
  555.     //if (fTabSelector) fTabSelector->suicide();  // is this delete causing bombs? YES, in motif
  556.     fTabSelector= new DTabSelector( this);
  557.     fCurrentTracker= fTabSelector;
  558.  
  559.     DTableView::Click( mouse);
  560. }
  561.  
  562.  
  563.  
  564.  
  565. void DAlnView::Click(Nlm_PoinT mouse)
  566. {
  567.     long     row, col;
  568.  
  569.     //if (gLastCommand) { delete gLastCommand; gLastCommand= NULL; } //gLastCommand->Commit();    //??
  570.  
  571.     Nlm_CaptureSlateFocus((Nlm_SlatE) fPanel); //! need for CharHandler !
  572.  
  573.     PointToCell( mouse, row, col);
  574.     
  575.     SeqMeter(row,col);
  576.  
  577.     if (fMaskLevel<1 && IsSelected(row, col)) {
  578.         //if (fSlider) delete fSlider;  // is this delete causing bombs?
  579.         fSlider= new DAlnSlider();
  580.         fSlider->IAlnSlider( fDoc, this, this, 0);
  581.         fCurrentTracker= fSlider;
  582.         }
  583.     else {
  584.         //if (fTabSelector) fTabSelector->suicide();  // is this delete causing bombs? YES, in motif
  585. #if MASKS
  586.         if (fMaskLevel>0) 
  587.             fTabSelector= new DMaskSelector( this, fMaskLevel, fSeqList);
  588.         else
  589. #endif
  590.             fTabSelector= new DTabSelector( this);
  591.         fCurrentTracker= fTabSelector;
  592.         }
  593.  
  594.     DTableView::Click( mouse);
  595. }
  596.  
  597.  
  598. void DAlnView::TrackMouse( short aTrackPhase,
  599.                     Nlm_PoinT& anchorPoint, Nlm_PoinT& previousPoint,
  600.                     Nlm_PoinT& nextPoint,    Nlm_Boolean mouseDidMove)
  601. {
  602.     DTableView::TrackMouse( aTrackPhase, anchorPoint, previousPoint, nextPoint,mouseDidMove); 
  603. }
  604.  
  605.  
  606. void DAlnView::TrackFeedback( short aTrackPhase,
  607.                     const Nlm_PoinT& anchorPoint, const Nlm_PoinT& previousPoint,
  608.                     const Nlm_PoinT& nextPoint, Nlm_Boolean mouseDidMove, Nlm_Boolean turnItOn)
  609. {
  610. #if 1
  611.     long     row, col;
  612.     if (mouseDidMove && aTrackPhase == DTracker::trackContinue ) { // 2 == trackContinue
  613.         PointToCell( nextPoint, row, col);
  614.         SeqMeter(row,col);
  615.         }
  616. #endif
  617.     DTableView::TrackFeedback( aTrackPhase, anchorPoint, previousPoint, nextPoint,
  618.                     mouseDidMove, turnItOn);
  619. }
  620.  
  621.  
  622. void DAlnIndex::DoubleClickAt(long row, long col)
  623. {
  624.     DSequence* ag= fSeqList->SeqAt(row); 
  625.     if (ag) fDoc->OpenSeqedWindow(ag);
  626. }
  627.  
  628. void DAlnITitle::DoubleClickAt(long row, long col)
  629. {
  630. }
  631.  
  632.  
  633.  
  634. void DAlnView::SeqMeter(long row, long col)
  635. {
  636.     // display base# location of mouse
  637.     if (fDoc->fSeqMeter) {
  638.         char basen[80];
  639.         sprintf(basen,"%d",col+1);
  640.         fDoc->fSeqMeter->SetTitle( basen);
  641.         }
  642. }
  643.  
  644. void DAlnView::SingleClickAt(long row, long col)
  645. {
  646. #if ETEXT
  647.     if (!(fLocked || gKeys->shift() || gKeys->command() || gKeys->option() )) {  
  648.         SetEmptySelection( true); 
  649.         if (fDoc->fAlnIndex) fDoc->fAlnIndex->SetEmptySelection( true); 
  650.         InstallEditSeq(row,col,col,false);
  651.         }
  652.     else
  653. #endif
  654. #if MASKS
  655.     if (fMaskLevel>0) {
  656.         Nlm_RecT r;
  657.         DSequence* ag= fSeqList->SeqAt(row); 
  658.         if (ag) {
  659.             if (gKeys->shift()) ag->SetMaskAt(col,fMaskLevel);
  660.             else ag->FlipMaskAt(col,fMaskLevel);
  661.             }
  662.         GetCellRect( row, col, r);
  663.         InvalRect( r);
  664.         }
  665.     else
  666. #endif
  667.          DTableView::SingleClickAt(row,  col);
  668.      //fDoc->SetViewMenu();
  669. }
  670.  
  671. void DAlnIndex::SingleClickAt(long row, long col)
  672. {
  673.      DTableView::SingleClickAt(row,  col);
  674.      //fDoc->SetViewMenu();
  675. }
  676.  
  677. void DAlnITitle::SingleClickAt(long row, long col)
  678. {
  679. }
  680.  
  681. void DAlnHIndex::Click(Nlm_PoinT mouse)
  682. {
  683.     DPanel::Click(mouse);
  684.     fLastCol= -1;
  685.     ClickColumn(mouse);
  686. }
  687.     
  688. void DAlnHIndex::ClickColumn(Nlm_PoinT mouse)
  689. {
  690. #if MASKS
  691.     long row, col;
  692.     // if (fMouseStillDown && ...)
  693.     short masklevel= fDoc->fAlnView->fMaskLevel;
  694.     fDoc->fAlnView->PointToCell( mouse, row, col);
  695.     
  696.     if (col != fLastCol && col >= 0 && col < fDoc->fAlnView->GetMaxCols()) {
  697.         Nlm_RecT r;
  698.         long irow, nrow= fSeqList->GetSize();
  699.         if (masklevel == 0) {
  700.             r.left= col;
  701.             r.right= col+1;
  702.             r.top= 0;
  703.             r.bottom= fDoc->fAlnView->GetMaxRows(); 
  704.             long ext;
  705.             if (gKeys->shift()) ext=  DTabSelection::kExtendSingle;
  706.             else if (gKeys->command()) ext=  DTabSelection::kExtendMulti;
  707.             else ext=  DTabSelection::kDontExtend;
  708.             fDoc->fAlnView->InvalidateSelection();
  709.             fDoc->fAlnView->SelectCells( r, ext, 
  710.                             ! DTabSelection::kHighlight,  DTabSelection::kSelect);
  711.             fDoc->fAlnView->InvalidateSelection();
  712.             }
  713.         else for (irow=0; irow<nrow; irow++) {
  714.             DSequence* ag= fSeqList->SeqAt(irow); 
  715.             if (ag) {
  716.                 if (gKeys->shift()) ag->SetMaskAt(col,masklevel);
  717.                 else ag->FlipMaskAt(col,masklevel);
  718.                 }
  719.             fDoc->fAlnView->GetCellRect( irow, col, r);
  720.             fDoc->fAlnView->InvalRect(r); //InvalRect(r);
  721.             }
  722.         fLastCol= col;
  723.         }
  724.         
  725. #endif
  726. }
  727.     
  728.  
  729.  
  730. void DAlnView::Scroll(Boolean vertical, DView* scrollee, long newval, long oldval)
  731. {
  732.     long diff= newval - oldval;
  733.  
  734.     DTableView::Scroll(vertical, scrollee, newval, oldval);
  735.     
  736.     if (diff && fEditSeq && fEditSeq->fVisible) {
  737.         Nlm_RecT    r, oldr;
  738.         long delta;
  739.         if (vertical) {
  740.             delta= diff * fItemHeight; // bad for variable line height
  741.             fEditSeq->GetPosition( r);
  742.             oldr= r;
  743.             Nlm_OffsetRect( &r, 0, -delta);
  744.             if (r.top < fRect.top || r.top >= fRect.bottom) {
  745.                 //DeInstallEditSeq();
  746.                 fEditSeq->HideEdit();
  747.                 fDoc->SetEditText(NULL); //??
  748.                 InvalRect(r);
  749.                 }
  750.             else {
  751.                 fEditSeq->SetPosition( r);
  752.                     // !!! must re-install editseq if it gets back in view !!
  753.                 if (oldr.top < fRect.top || oldr.top >= fRect.bottom) {
  754.                     fEditSeq->ShowEdit(false);
  755.                     fDoc->SetEditText(fEditSeq); 
  756.                     InvalRect(r); //??
  757.                     }
  758.                 }
  759.             }
  760.         else {
  761. #if 1
  762.                 // messy, undo bitmap scroll by TableView for just editseq rect...
  763.             fEditSeq->GetPosition(r);
  764.             delta= diff * GetItemWidth(0); //fItemWidth; //gAlnCharWidth
  765.             Nlm_ScrollRect(&r, delta, 0);
  766.                     // then redo scroll using edit method !
  767.             fEditSeq->Scroll(vertical, scrollee, newval, oldval);
  768. #endif
  769.             }
  770.         }
  771.         
  772.     if (scrollee == this) {
  773.         if (vertical) {
  774.             fDoc->fAlnIndex->Scroll(vertical, scrollee, newval, oldval);
  775.             }
  776.         else {
  777.             fDoc->fAlnHIndex->Scroll(vertical, scrollee, newval, oldval);
  778.             }
  779.         }
  780. }
  781.  
  782. void DAlnIndex::Scroll(Boolean vertical, DView* scrollee, long newval, long oldval)
  783. {
  784.     DTableView::Scroll(vertical, scrollee, newval, oldval);
  785.     if (scrollee == this) {
  786.         if (vertical) {
  787.             fDoc->fAlnView->Scroll(vertical, scrollee, newval, oldval);
  788.             }
  789.         else   // horizontal
  790.             fDoc->fAlnITitle->Scroll(vertical, scrollee, newval, oldval);
  791.              
  792.         }
  793. }
  794.  
  795.  
  796. void DAlnHIndex::Scroll(Boolean vertical, DView* scrollee, long newval, long oldval)
  797. {
  798.     Nlm_RecT    r;
  799.     long delta;
  800.     long diff= newval-oldval;
  801.     ViewRect(r);
  802.     this->Select(); // need for motif !
  803.     if (vertical) {
  804. #if 0
  805.         delta= diff * cHeight;
  806.         Nlm_ScrollRect (&r, 0, -delta);
  807.         if (diff<0) r.bottom= r.top - delta + Min(5,-delta/2);
  808.         else r.top= r.bottom - delta - Min(5,delta/2);
  809. #endif
  810.         }
  811.     else {
  812. #if 1
  813.         short cWidth= gAlnCharWidth;
  814.         delta= diff * cWidth;
  815.         Nlm_ScrollRect (&r, -delta, 0);
  816.             // test fix for off-by-a-few-pixels in scroll...
  817.         if (diff<0) r.right= r.left - delta + Min(5,-delta/2);
  818.         else r.left= r.right - delta - Min(5,delta/2);
  819. #endif
  820.         }
  821.     this->InvalRect( r);
  822.  
  823.     if (scrollee == this) {
  824.         if (!vertical) fDoc->fAlnView->Scroll(vertical, scrollee, newval, oldval);
  825.         }
  826. }
  827.  
  828.  
  829.  
  830. DSequence* DAlnView::SelectedSequence(long& selectedRow)
  831. {
  832.     if (GetSelectedRow() !=  DTabSelection::kNoSelection) {
  833.         selectedRow= GetSelectedRow();
  834.         return fSeqList->SeqAt(selectedRow);
  835.         }
  836.     else if (fDoc && fDoc->fAlnIndex->GetSelectedRow() !=  DTabSelection::kNoSelection) {
  837.         selectedRow= fDoc->fAlnIndex->GetSelectedRow();
  838.         return fSeqList->SeqAt(selectedRow);    
  839.         }
  840. #if 1
  841.     else if (fEditSeq && fEditRow>=0) {
  842.         selectedRow= fEditRow;
  843.         return fSeqList->SeqAt(selectedRow);    
  844.         }
  845. #endif
  846.     else {
  847.         selectedRow=  DTabSelection::kNoSelection;
  848.         return NULL;
  849.         }
  850. }
  851.  
  852.  
  853. void DAlnView::UpdateSize()
  854. {    
  855.     long diff;   
  856.     if (fSeqList) diff= fSeqList->GetSize() - GetMaxRows();
  857.     else diff= -GetMaxRows();
  858.     ChangeRowSize( -1, diff);  // -1 prevents redraw..else use fMaxRows    
  859.     if (fDoc && fDoc->fAlnIndex) fDoc->fAlnIndex->ChangeRowSize(-1, diff);
  860.  
  861. #if MASKS
  862.     if (diff>0) { //fMaskLevel>0 && 
  863.         long i, nseq= fSeqList->GetSize();
  864.         for (i=nseq-diff; i<nseq; i++) {
  865.             DSequence* aseq= fSeqList->SeqAt(i);
  866.             if (fMaskLevel>0 || aseq->Masks()) aseq->FixMasks();
  867.             }
  868.         }
  869. #endif
  870. }
  871.  
  872.  
  873. void DAlnView::UpdateWidth(DSequence* aSeq)
  874.     long alnlen= aSeq->LengthF() + 30;
  875.     if (alnlen > GetMaxCols()) ChangeColSize( -1, alnlen-GetMaxCols());  
  876.     SetItemWidth( 0, GetMaxCols(), gAlnCharWidth); 
  877. #if MASKS
  878.     if (fMaskLevel>0 || aSeq->Masks()) aSeq->FixMasks();
  879. #endif
  880.     //if (fDoc && fDoc->fAlnHIndex) fDoc->fAlnHIndex->UpdateWidth(); //keep ruler in sync
  881. }
  882.  
  883. void DAlnView::UpdateAllWidths()
  884. {
  885.     long alnlen= 0;
  886.     long i, nseq= fSeqList->GetSize();
  887.     for (i=0; i<nseq; i++) {
  888.         DSequence* aSeq= fSeqList->SeqAt(i);
  889.         alnlen= Max(alnlen, aSeq->LengthF()); 
  890. #if MASKS
  891.         if (fMaskLevel>0 || aSeq->Masks()) aSeq->FixMasks();
  892. #endif
  893.         }
  894.     alnlen += 30; 
  895.     if (alnlen > GetMaxCols()) ChangeColSize( -1, alnlen-GetMaxCols());
  896.     SetItemWidth( 0, GetMaxCols(), gAlnCharWidth);  
  897.     //if (fDoc && fDoc->fAlnHIndex) fDoc->fAlnHIndex->UpdateWidth(); //keep ruler in sync
  898. }
  899.  
  900. void DAlnView::SetTextLock( Boolean turnon)
  901. {
  902.     fLocked= turnon;
  903.     if (fLocked) DeInstallEditSeq();
  904. }
  905.  
  906.  
  907. void DAlnView::SetViewMode(short viewmode) 
  908. {
  909.     switch (viewmode) {
  910.         case kModeSlide: 
  911.             this->SetTextLock( true);
  912.             if (this->fMaskLevel>0) this->Invalidate();
  913.             this->fMaskLevel= 0; 
  914.             break;
  915.             
  916.         case kModeEdit:  
  917.             this->SetTextLock( false);
  918.             if (this->fMaskLevel>0) this->Invalidate();
  919.             this->fMaskLevel= 0; 
  920.             break;
  921.             
  922.         case kModeMask1: 
  923.         case kModeMask2: 
  924.         case kModeMask3: 
  925.         case kModeMask4: {
  926.             this->SetTextLock( true);
  927.             this->fMaskLevel= viewmode - kModeMask1 + 1; 
  928.             this->Invalidate();
  929.             long i, nseq= fSeqList->GetSize();
  930.             for (i=0; i<nseq; i++) fSeqList->SeqAt(i)->FixMasks();
  931.             }
  932.             break;
  933.             
  934.         default :
  935.             break;
  936.         }
  937. }
  938.  
  939. void DAlnView::SetViewColor(short colorkind) 
  940. {
  941.     if (colorkind > kBaseVarLite) {
  942.         gSwapBackground= true; // temp set
  943.         colorkind -= 2; // hack !
  944.         }
  945.     else {
  946.         gSwapBackground= false;
  947.         }
  948.  
  949.     switch (colorkind) {
  950. #if 0
  951.         case kBaseBackColor: 
  952.             gSwapBackground= true; 
  953.             break;
  954. #endif
  955.         case kBaseBlack: 
  956.         case kBaseColor:  
  957.         case kBaseVarLite: 
  958.         default :
  959.             //gSwapBackground= false;
  960.             break;
  961.         }
  962.     
  963.     //if (fColorBases != colorkind) 
  964.     Invalidate();
  965.     fColorBases= colorkind;
  966. }
  967.  
  968.  
  969. void DAlnView::CharHandler(char c) 
  970. {
  971.     switch (c) {
  972.     
  973.         case 0x08:    // backspace
  974.         case 0x7f:     // Delete (but mapped to backspace here...)
  975.             // delete selected softindels... == degap
  976.             (void) fDoc->DoMenuTask( DSeqDoc::cDegap, NULL);
  977.             break;
  978.             
  979.         case 0x0D: // return // 0x03 == enter
  980.         case 0x09: // tab
  981.       case 0x1C: // <- arrow
  982.         case 0x1D: // -> arrow
  983.         case 0x1E: // ^ arrow
  984.         case 0x1F: // v arrow
  985.         default: 
  986.             break; 
  987.         }
  988. }
  989.  
  990.  
  991.  
  992.  
  993. // move this to DSeqCmds.h ...
  994. class DSetEditCmd : public DSeqChangeCmd {
  995. public: 
  996.     DSetEditCmd( DSeqDoc* itsAlnDoc, DView* itsView, DSequence* oldSeq, char*& newbases):
  997.          DSeqChangeCmd("edit seq", itsAlnDoc, itsView, NULL) 
  998.          {
  999.             fOldSeqs= new DSeqList(); 
  1000.              fOldSeqs->InsertLast( oldSeq);
  1001.             DSequence* newSeq= (DSequence*)oldSeq->Clone();
  1002.             newSeq->SetBases(newbases, false);
  1003.             newSeq->SetChanged(true);
  1004.             fNewSeqs->InsertLast( newSeq);
  1005.          }
  1006. };
  1007.  
  1008.  
  1009. void DAlnView::DeInstallEditSeq()
  1010. {
  1011.     if (fEditRow >= 0) { 
  1012.         if (fEditSeq) {
  1013.             Nlm_RecT r;
  1014.             Boolean didedit= false;
  1015.             char* newbases = fEditSeq->GetText();
  1016.             if (StringCmp(newbases, fEditSeq->fSeq->Bases()) != 0) {
  1017.                 didedit= true;
  1018.                 DSetEditCmd* cmd= new DSetEditCmd( fDoc, this, fEditSeq->fSeq, newbases);
  1019.                 if (cmd) PostTask(cmd); 
  1020.                 }
  1021.             MemFree( newbases);
  1022.           
  1023.             fEditSeq->GetPosition(r);
  1024.             fEditSeq->HideEdit();
  1025.             fDoc->SetEditText(NULL); //??
  1026.             InvalRect(r);
  1027.             if (didedit && fDoc->fAlnIndex) {
  1028.                 fDoc->fAlnIndex->GetRowRect( fEditRow, r);
  1029.                 fDoc->fAlnIndex->InvalRect( r);
  1030.                 }
  1031.             }
  1032.         fEditRow= -1;
  1033.         }
  1034.  
  1035. }
  1036.  
  1037.  
  1038.  
  1039. void DAlnView::InstallEditSeq(long row, long selStart, long selEnd, Boolean doLight)
  1040. {
  1041.          
  1042.     DeInstallEditSeq();
  1043.     //if (gLastEditView && gLastEditView != this) gLastEditView->DeInstallEditSeq();
  1044.  
  1045.     fEditRow= row;
  1046.     if (row >= 0) {
  1047.         Nlm_RecT r;
  1048.         DSequence* aSeq= fSeqList->SeqAt( row);
  1049.         this->GetRowRect( row, r);
  1050.         
  1051.         if (r.left > 0) r.left--; // fix off-by-one!?
  1052.         long cwidth= (r.right - r.left) / GetItemWidth(0);
  1053.         long oldval= -1; // force scroll/setposition
  1054.         long leftoff= GetLeft();
  1055.         
  1056. #ifdef WIN_MOTIF
  1057.             // must install new editseq each time -- otherwiz trash-city...
  1058.         if (fEditSeq) delete fEditSeq;
  1059.         fEditSeq= new DAlnSequence( 0, this, cwidth); 
  1060. #endif
  1061.         fEditSeq->fSeq= aSeq;
  1062.         fEditSeq->SetPosition( r);
  1063.         fEditSeq->ShowEdit(true);
  1064.         fEditSeq->SetSelection( selStart, selEnd);
  1065. #ifdef WIN_MAC
  1066.             // mac - off-by-one if oldvalue=-1 !!
  1067.         if (leftoff>0) fEditSeq->Scroll(false, NULL,leftoff, 0);
  1068. #else
  1069.             // motif - off-by-one if oldvalue=0 !!
  1070.         fEditSeq->Scroll(false, NULL, leftoff, oldval);
  1071. #endif
  1072.         fDoc->SetEditText(fEditSeq); //done by SetSelection??
  1073.  
  1074.         if (gLastCommand) { delete gLastCommand; gLastCommand= NULL; } //gLastCommand->Commit();    //??
  1075.         //gLastEditView= this;
  1076.         }
  1077.  
  1078. }
  1079.  
  1080.  
  1081. void DAlnView::registerInsertLast( DSequence* aSeq)
  1082. {
  1083.     ChangeRowSize( GetMaxRows(), 1);
  1084.     if (fDoc && fDoc->fAlnIndex) 
  1085.         fDoc->fAlnIndex->ChangeRowSize( GetMaxRows(), 1);
  1086.     UpdateWidth(aSeq);    
  1087. }
  1088.  
  1089. void DAlnView::addToAlnList( DSequence* aSeq)
  1090. {
  1091.     fSeqList->InsertLast( aSeq);
  1092.     //aSeq->fIndex= fSeqList->GetSize(); //??
  1093.     registerInsertLast( aSeq); 
  1094.  
  1095. #if NOT_NOW_MASKS
  1096.         // done via registerInsertLast -> UpdateWidth
  1097.         if (fMaskLevel>0 || aSeq->fMasks) aSeq->FixMasks();
  1098. #endif
  1099. }
  1100.  
  1101.  
  1102. void DAlnView::MakeConsensus()
  1103.     fSeqList->MakeConsensus();
  1104.     long arow= fSeqList->ConsensusRow();
  1105.     if (arow>0) {
  1106.         Nlm_RecT r;
  1107.         GetRowRect( arow, r);
  1108.         this->InvalRect( r);
  1109.         }
  1110. }
  1111.  
  1112.  
  1113. char* DAlnView::FindCommonBases( short minCommonPerCent)
  1114. {
  1115. // THIS_IS_OBSOLETE
  1116.     char *hCommon, *hFirst = NULL;
  1117.     //gMinCommonPercent= minCommonPerCent;
  1118.     hCommon= fSeqList->FindCommonBases(DSeqList::gMinCommonPercent, hFirst);
  1119.     if (hFirst) MemFree( hFirst);
  1120.     return hCommon;
  1121. }
  1122.  
  1123.  
  1124. void DAlnView::HiliteORFs()
  1125. {
  1126.     long    start, stop;
  1127.     long nseq= fSeqList->GetSize();
  1128.     if (fMaskLevel>0) 
  1129.     for (long iseq= 0; iseq<nseq; iseq++) {
  1130.         DSequence* aSeq= fSeqList->SeqAt(iseq);
  1131.         long alen= aSeq->LengthF();
  1132.         aSeq->ClearMask(fMaskLevel);
  1133.         aSeq->ClearSelection();
  1134.  
  1135.         aSeq->SearchORF( start, stop);
  1136.         while (start>=0) {
  1137.             if (stop<start) stop= alen;
  1138.             for (long ibase=start; ibase<=stop; ibase++) 
  1139.                 aSeq->SetMaskAt( ibase, fMaskLevel);
  1140.             if (stop >= alen) start= -1;
  1141.             else {
  1142.               aSeq->SetSelection( stop+1, alen);
  1143.                 aSeq->SearchORF( start, stop);
  1144.                 }
  1145.             }
  1146.         } 
  1147.     this->Invalidate();
  1148. }
  1149.  
  1150.  
  1151. Boolean DAlnView::IsMasked()
  1152. {  
  1153.     if (fMaskLevel>0) {
  1154.         long iseq, nseq= fSeqList->GetSize();
  1155.         for ( iseq= 0; iseq<nseq; iseq++) {
  1156.             DSequence* aseq= fSeqList->SeqAt(iseq);
  1157.             long jbase, nbases= aseq->LengthF();
  1158.             if (aseq->MasksOk())
  1159.              for (jbase= 0; jbase<nbases; jbase++) {
  1160.                 if ( aseq->MaskAt(jbase, fMaskLevel) > 0) 
  1161.                     return true;
  1162.                 }
  1163.             }
  1164.         }
  1165.     return false;
  1166. }
  1167.  
  1168.  
  1169. void DAlnView::ReplicateMask()
  1170. {  
  1171.     if (fMaskLevel > 0 && fDoc && fDoc->fAlnIndex) {
  1172.       long refRow= fDoc->fAlnIndex->GetSelectedRow();
  1173.         if (refRow !=  DTabSelection::kNoSelection) {
  1174.             DSequence* refSeq = fSeqList->SeqAt(refRow);
  1175.             long nseq= fSeqList->GetSize();
  1176.             if (refSeq) 
  1177.             for (long iseq= 0; iseq<nseq; iseq++)
  1178.              if (iseq != refRow) {
  1179.                 DSequence* aSeq= fSeqList->SeqAt(iseq);
  1180.                 long alen= aSeq->LengthF();
  1181.                 for (long ibase=0; ibase<alen; ibase++) {
  1182.                     long val= refSeq->MaskAt(ibase, fMaskLevel);
  1183.                     aSeq->SetMaskAt( ibase, fMaskLevel, val);
  1184.                     }
  1185.                 }
  1186.             this->Invalidate();
  1187.             }
  1188.         }
  1189. }
  1190.  
  1191. Boolean DAlnView::MaskCommand(short command)
  1192. {  
  1193.     //if (menuid == kSeqMaskMenu && fMaskLevel>0)   
  1194.     
  1195.         long iseq, nseq= fSeqList->GetSize();
  1196.         long jbase, nbases;
  1197.         short maskval, selval;
  1198.         
  1199.          switch (command) {
  1200.          
  1201.              case DSeqDoc::cMaskReplicate:
  1202.                  ReplicateMask();
  1203.                  return true;
  1204.                  
  1205.             case DSeqDoc::cMaskSelCommon:
  1206.                 HiliteCommonBases();
  1207.                 return true;
  1208.                 
  1209.             case DSeqDoc::cMaskSelORF:
  1210.                 HiliteORFs();
  1211.                 return true;
  1212.                 
  1213.             case DSeqDoc::cMaskSelAll:
  1214.                 for (iseq= 0; iseq<nseq; iseq++) 
  1215.                     fSeqList->SeqAt(iseq)->SetMask(fMaskLevel);
  1216.                 Invalidate();
  1217.                 return true;
  1218.                 
  1219.             case DSeqDoc::cMaskInvert:
  1220.                 for (iseq= 0; iseq<nseq; iseq++) 
  1221.                     fSeqList->SeqAt(iseq)->FlipMask(fMaskLevel);
  1222.                 Invalidate();
  1223.                 return true;
  1224.                 
  1225.             case DSeqDoc::cMaskClear:
  1226.                 for (iseq= 0; iseq<nseq; iseq++) 
  1227.                     fSeqList->SeqAt(iseq)->ClearMask(fMaskLevel);
  1228.                 Invalidate();
  1229.                 return true;
  1230.  
  1231.             case DSeqDoc::cSel2Mask:
  1232.                 for (iseq= 0; iseq<nseq; iseq++) {
  1233.                     DSequence* aseq= fSeqList->SeqAt(iseq);
  1234.                     nbases= aseq->LengthF();
  1235.                     for (jbase= 0; jbase<nbases; jbase++) {
  1236.                         selval= IsSelected(iseq,jbase);
  1237.                         aseq->SetMaskAt(jbase, fMaskLevel, selval);
  1238.                         }
  1239.                     }
  1240.                 Invalidate();
  1241.                 return true;
  1242.  
  1243.             case DSeqDoc::cMask2Sel:
  1244.             case DSeqDoc::cMaskOrSel:
  1245.             case DSeqDoc::cMaskAndSel:
  1246.                 for (iseq= 0; iseq<nseq; iseq++) {
  1247.                     DSequence* aseq= fSeqList->SeqAt(iseq);
  1248.                     nbases= aseq->LengthF();
  1249.                     for (jbase= 0; jbase<nbases; jbase++) {
  1250.                         maskval= aseq->MaskAt(jbase, fMaskLevel);
  1251.                         selval = IsSelected(iseq,jbase);
  1252.                         switch (command) {
  1253.                           case DSeqDoc::cMask2Sel    : selval= maskval; break;
  1254.                           case DSeqDoc::cMaskOrSel: selval= (selval | maskval); break;
  1255.                           case DSeqDoc::cMaskAndSel: selval= (selval & maskval); break;
  1256.                           }
  1257.                           
  1258. // !! This IS NO GOOD now -- must have disjoint selection ability in DTableView...
  1259.                         SelectCells(iseq,jbase, DTabSelection::kExtendMulti,false, selval);
  1260.                         }
  1261.                     }
  1262.                 Invalidate();
  1263.                 return true;
  1264.                 
  1265.             default: 
  1266.                 return true;
  1267.             }
  1268. }
  1269.  
  1270.  
  1271.  
  1272.  
  1273. void DAlnView::HiliteCommonBases()
  1274. {  
  1275. #if 1
  1276.     char * hFirst = NULL;
  1277.     char * hCommon= fSeqList->FindCommonBases(DSeqList::gMinCommonPercent, hFirst);
  1278.     
  1279.     long nseq= fSeqList->GetSize();
  1280.     if (fMaskLevel>0) 
  1281.     for (long iseq= 0; iseq<nseq; iseq++) {
  1282.         DSequence* aSeq= fSeqList->SeqAt(iseq);
  1283.         long alen= aSeq->LengthF();
  1284.         char * hf = hFirst;
  1285.         char * hc = hCommon;
  1286.         char * bc = aSeq->Bases();
  1287.         aSeq->ClearMask(fMaskLevel);
  1288.         for (long ibase=0; ibase<alen; ibase++, hf++, hc++, bc++) 
  1289.             if (iseq >= (unsigned char) *hf && toupper(*bc) == *hc)
  1290.                 aSeq->SetMaskAt( ibase, fMaskLevel);
  1291.         }
  1292.     this->Invalidate();
  1293.     MemFree(hCommon);
  1294.     MemFree(hFirst);
  1295. #endif
  1296. }
  1297.  
  1298.  
  1299. #define DRAWCLASS 1
  1300. #ifndef DRAWCLASS
  1301.  
  1302. void DAlnView::DrawAlnInStyle( baseColors colors, Boolean swapBackColor,
  1303.                                                             char*    pText, long indx, long len, long row)
  1304. {
  1305. #if MASKS
  1306.         // ?? do we want this? main use of styles is in PrettyPrint...
  1307.     Nlm_PoinT pt;
  1308.     long            endx, pend, skip;
  1309.     char            ch, lastch;
  1310.     Nlm_RecT    crec; // set this to rect about draw char...
  1311.     short         maskval;
  1312.         
  1313.     Boolean dostyles= (fCurSeq && fCurSeq->MasksOk());
  1314.     if (!dostyles) {
  1315.         DrawAlnColors( colors, swapBackColor, pText, indx, len,row);
  1316.         return;
  1317.         }
  1318.         
  1319.     long cw= GetItemWidth(0);
  1320.     Nlm_GetPen( &pt);
  1321.     if (pt.x < 0) {
  1322.         skip= (-pt.x) / cw;
  1323.       indx += skip;
  1324.         len -= skip;
  1325.         pt.x += skip*cw;
  1326.         Nlm_MoveTo( pt.x, pt.y);
  1327.         }
  1328.     pend= len * cw;
  1329.     
  1330.     if (pend > GetRect().right) {   
  1331.         skip= (pend - GetRect().right) / cw;  
  1332.         len -= skip;
  1333.         }
  1334.     endx= indx + len - 1;
  1335.     
  1336.     lastch= 0;
  1337.     long cht= GetItemHeight(0);
  1338.     for (long i= indx; i<=endx; i++) {
  1339.         ch= pText[i];
  1340.         if (ch >= ' ') {
  1341.             if (ch!=lastch) Nlm_SetColor( colors[ch-' ']);
  1342.             Nlm_PaintChar(ch);
  1343.             lastch= ch;
  1344.             if (fMaskLevel>0) {
  1345.                 maskval= fCurSeq->MaskAt(i, fMaskLevel);
  1346.                 if (maskval>0) {
  1347.                     Nlm_LoadRect( &crec, pt.x, pt.y-cht+2, pt.x+cw, pt.y+2); //??
  1348.                     //Nlm_FrameRect( &crec); //?? doesn't show on XMotif?
  1349.                     DTableView::InvertRect( crec); 
  1350.                     }
  1351.                 }
  1352.             pt.x += cw;
  1353.             // !? must reset some of drawing environ !? > Font !
  1354.             }
  1355.         }
  1356.     Nlm_Black();
  1357.     
  1358. #else
  1359.     DrawAlnColors( colors, swapBackColor, pText, indx, len, row);
  1360. #endif
  1361. }
  1362.  
  1363.  
  1364. void DAlnView::DrawAlnColors(baseColors colors, Boolean swapBackColor,
  1365.                                                             char*    pText, long indx, long len, long row)
  1366. {
  1367.     Nlm_PoinT pt;
  1368.     long            endx, pend, skip;
  1369.     char            ch, lastch;
  1370.     
  1371.     long cw= GetItemWidth(0); 
  1372.     Nlm_GetPen( &pt);
  1373.     if (pt.x < 0) {
  1374.         skip= (-pt.x) / cw;
  1375.       indx += skip;
  1376.         len -= skip;
  1377.         Nlm_MoveTo( pt.x + skip*cw, pt.y);
  1378.         }
  1379.     pend= len * cw;
  1380.     
  1381.     if (pend > GetRect().right) {   
  1382.         skip= (pend - GetRect().right) / cw;  
  1383.         len -= skip;
  1384.         }
  1385.     endx= indx + len - 1;
  1386.     
  1387.     //-- backcolor not showing -- need erase or something...
  1388.     //if (swapBackcolor) RGBForeColor( colors[' '-' ']); else RGBBackColor( colors[' '-' ']);
  1389.      
  1390.     //Nlm_CopyMode(); //?? assume?
  1391.     lastch= 0;
  1392.     for (long i= indx; i<=endx; i++) {
  1393.         ch= pText[i];
  1394.         if (ch >= ' ') {
  1395.             // if (swapBackcolor) RGBBackColor( colors[ch-' ']); else 
  1396.             if (ch!=lastch) Nlm_SetColor( colors[ch-' ']);
  1397.             Nlm_PaintChar(ch);
  1398.             lastch= ch;
  1399.             }
  1400.         }
  1401.     Nlm_Black();
  1402.  
  1403. #else
  1404. ///////////////////////////
  1405.  
  1406.  
  1407. void DAlnView::DrawAlnColors(baseColors colors, Boolean swapBackColor,
  1408.                                                             char*    pText, long indx, long len, long row)
  1409. {
  1410. }
  1411.  
  1412. void DAlnView::DrawAlnInStyle( baseColors colors, Boolean swapBackColor,
  1413.                                                             char*    pText, long indx, long len, long row)
  1414. {
  1415. }
  1416.  
  1417. class DrawBases
  1418. {
  1419. public:
  1420.     Nlm_PoinT pt;
  1421.     long        endx, pend, indx, len, cw, cht;
  1422.     long        row, ytop, ybot;
  1423.     char        ch, lastch, *pText;
  1424.     DAlnView* tview;
  1425.     Boolean  swapBackcolor;
  1426.     //baseColors&    colors;
  1427.     
  1428.     DrawBases() {}
  1429.     virtual void doDraw(DAlnView* theView, baseColors theColors, Boolean backcolor,
  1430.                char*    theText, long theIndx, long theLen, long theRow);
  1431.     virtual void Calc();
  1432.     virtual void DrawSub(baseColors colors);
  1433.     virtual void InvertSel();
  1434. };
  1435.  
  1436. class DrawGreyBases : public DrawBases
  1437. {
  1438. public:    
  1439.     DrawGreyBases() {}
  1440.     virtual void DrawSub(baseColors colors);
  1441. };
  1442.  
  1443. class DrawBasesWithMask : public DrawBases
  1444. {
  1445. public:
  1446.     Boolean fIsGrey;
  1447.  
  1448.     DrawBasesWithMask() : fIsGrey(false) {}
  1449.     virtual void doDraw(DAlnView* theView, baseColors theColors, Boolean backcolor,
  1450.                char*    theText, long theIndx, long theLen, long theRow);
  1451.     virtual void DrawWithMasks(baseColors colors);
  1452.     virtual void DrawSub(baseColors colors);
  1453. };
  1454.  
  1455.  
  1456. class DrawGreyBasesWithMask : public DrawBasesWithMask
  1457. {
  1458. public:
  1459.     DrawGreyBasesWithMask() { fIsGrey = true; }
  1460. };
  1461.  
  1462.  
  1463.  
  1464. void DrawBases::doDraw(DAlnView* theView, baseColors theColors, Boolean backcolor,
  1465.            char*    theText, long theIndx, long theLen, long theRow)
  1466. {
  1467.     tview= theView;
  1468.     //colors= theColors;
  1469.     pText= theText;
  1470.     indx= theIndx;
  1471.     len= theLen;
  1472.     row= theRow;
  1473.     lastch= 0;
  1474.     swapBackcolor= backcolor;
  1475.     if (swapBackcolor) Nlm_CopyMode();
  1476.     
  1477.     Calc();
  1478.     DrawSub(theColors);
  1479.     InvertSel();
  1480.     if (swapBackcolor) Nlm_MergeMode();
  1481. }
  1482.  
  1483. void DrawBases::Calc() 
  1484. {
  1485.     long skip;
  1486.     cht= tview->GetItemHeight(0);
  1487.     cw = tview->GetItemWidth(0); 
  1488.     Nlm_GetPen( &pt);
  1489.     ytop= pt.y-cht+2;
  1490.     ybot= pt.y+2;  
  1491.     if (pt.x < 0) {
  1492.         skip= (-pt.x) / cw;
  1493.       indx+= skip;
  1494.         len -= skip;
  1495.         Nlm_MoveTo( pt.x + skip*cw, pt.y);
  1496.         }
  1497.     pend= len * cw;
  1498.     
  1499.     if (pend > tview->GetRect().right) {   
  1500.         skip= (pend - tview->GetRect().right) / cw;  
  1501.         len -= skip;
  1502.         }
  1503.     endx= indx + len - 1;
  1504. }
  1505.  
  1506.  
  1507. void DrawBases::InvertSel()
  1508. {         
  1509.     Nlm_RecT crec;
  1510.     long atx= pt.x;
  1511.     
  1512.     for (long i= indx; i<=endx; i++) {
  1513.       if (tview->fSelection->IsSelected(row, i)) {
  1514.             Nlm_LoadRect( &crec, atx, ytop, atx+cw, ybot); 
  1515.             tview->DTableView::InvertRect( crec);
  1516.             }
  1517.         atx += cw;
  1518.         }
  1519. }
  1520.  
  1521. void DrawBases::DrawSub(baseColors colors)
  1522. {
  1523.     for (long i= indx; i<=endx; i++) {
  1524.         ch= pText[i];
  1525.         if (ch >= ' ') {
  1526.             if (ch!=lastch)  {
  1527. #ifdef BACKCOLOR
  1528.                 if (swapBackcolor) Nlm_SetBackColor( colors[ch-' ']);  else 
  1529. #endif
  1530.                 Nlm_SetColor( colors[ch-' ']);
  1531.                 }
  1532.             Nlm_PaintChar(ch);
  1533.             lastch= ch;
  1534.             }
  1535.         }
  1536.     Nlm_Black();
  1537. }
  1538.  
  1539. void DrawBasesWithMask::DrawSub(baseColors colors)
  1540. {
  1541.     for (long i= indx; i<=endx; i++) {
  1542.         ch= pText[i];
  1543.         if (ch >= ' ') {
  1544.             if (fIsGrey) {
  1545. #ifdef BACKCOLOR
  1546.                 if (swapBackcolor) Nlm_SetBackColor( tview->fColcolors[i-indx]);  else 
  1547. #endif
  1548.                 Nlm_SetColor( tview->fColcolors[i-indx]);
  1549.                 }
  1550.             else if (ch!=lastch) {
  1551. #ifdef BACKCOLOR
  1552.                 if (swapBackcolor) Nlm_SetBackColor( colors[ch-' ']);  else 
  1553. #endif
  1554.                 Nlm_SetColor( colors[ch-' ']);
  1555.                 }
  1556.             Nlm_PaintChar(ch);
  1557.             lastch= ch;
  1558.             }
  1559.         }
  1560.     Nlm_Black();
  1561. }
  1562.  
  1563. void DrawGreyBases::DrawSub(baseColors colors)
  1564. {         
  1565.     for (long i= indx; i<=endx; i++) {
  1566.         ch= pText[i];
  1567.         if (ch >= ' ') {
  1568.                 // could draw by columns & setcolor once/col for speed !
  1569. #ifdef BACKCOLOR
  1570.             if (swapBackcolor) Nlm_SetBackColor( tview->fColcolors[i-indx]);  else 
  1571. #endif
  1572.             Nlm_SetColor( tview->fColcolors[i-indx]);
  1573.             Nlm_PaintChar(ch);
  1574.             }
  1575.         }
  1576.     Nlm_Black();
  1577. }
  1578.  
  1579.  
  1580.  
  1581. void DrawBasesWithMask::doDraw(DAlnView* theView, baseColors theColors, Boolean backcolor,
  1582.            char*    theText, long theIndx, long theLen, long theRow)
  1583. {
  1584.     tview= theView;
  1585.     //colors= &theColors;
  1586.     pText= theText;
  1587.     indx= theIndx;
  1588.     len= theLen;
  1589.     row= theRow;
  1590.     lastch= 0;
  1591.     swapBackcolor= backcolor;
  1592.     if (swapBackcolor) Nlm_CopyMode();
  1593.     
  1594.     Calc();
  1595.     Boolean dostyles= (tview->fCurSeq && tview->fCurSeq->MasksOk());
  1596.     if (!dostyles) {
  1597.         DrawSub(theColors);
  1598.         InvertSel();
  1599.         }
  1600.     else 
  1601.         DrawWithMasks(theColors);
  1602.     if (swapBackcolor) Nlm_MergeMode();
  1603. }
  1604.  
  1605. void DrawBasesWithMask::DrawWithMasks(baseColors colors)
  1606. {
  1607.     Nlm_RecT    crec;  
  1608.     
  1609.     for (long i= indx; i<=endx; i++) {
  1610.         Boolean notInverted= true;
  1611.         ch= pText[i];
  1612.         if (ch >= ' ') {
  1613.             if (fIsGrey) {
  1614. #ifdef BACKCOLOR
  1615.                 if (swapBackcolor) Nlm_SetBackColor( tview->fColcolors[i-indx]);  else 
  1616. #endif
  1617.                 Nlm_SetColor( tview->fColcolors[i-indx]);
  1618.                 }
  1619.             else if (ch!=lastch) {
  1620. #ifdef BACKCOLOR
  1621.                 if (swapBackcolor) Nlm_SetBackColor( colors[ch-' ']);  else 
  1622. #endif
  1623.                 Nlm_SetColor( colors[ch-' ']);
  1624.                 }
  1625.             Nlm_PaintChar(ch);
  1626.             lastch= ch;
  1627.             if (tview->fMaskLevel>0) {
  1628.                 notInverted= false;
  1629.                 short maskval= tview->fCurSeq->MaskAt(i, tview->fMaskLevel);
  1630.                 if (maskval>0) {
  1631.                     Nlm_LoadRect( &crec, pt.x, ytop, pt.x+cw, ybot); //??
  1632.                     //Nlm_FrameRect( &crec); //?? doesn't show on XMotif?
  1633.                     tview->DTableView::InvertRect( crec); 
  1634.                     }
  1635.                 }
  1636.             if (notInverted) {
  1637.               if (tview->fSelection->IsSelected(row, i)) {
  1638.                     Nlm_LoadRect( &crec, pt.x, ytop, pt.x+cw, ybot); //??
  1639.                     tview->DTableView::InvertRect( crec);
  1640.                     }
  1641.                 }
  1642.             pt.x += cw;
  1643.             // !? must reset some of drawing environ !? > Font !
  1644.             }
  1645.         }
  1646.     Nlm_Black();
  1647. }
  1648.  
  1649.     
  1650.     
  1651. DrawBases         gDrawBases;
  1652. DrawBasesWithMask gDrawBasesWithMask;
  1653. //DrawGreyBases            gDrawGreyBases;
  1654. DrawGreyBasesWithMask    gDrawGreyBases;
  1655. #endif
  1656.  
  1657.  
  1658. void DAlnView::DrawAllColors(Nlm_RecT r, long row)
  1659. #ifdef DRAWCLASS
  1660. #define DRAWALN(a,b,c,d,e,f)    gDrawBasesWithMask.doDraw(this,a,b,c,d,e,f)
  1661. #else
  1662. #if MASKS
  1663. #define DRAWALN    DrawAlnInStyle
  1664. #else
  1665. #define DRAWALN    DrawAlnColors
  1666. #endif
  1667. #endif
  1668.  
  1669.     long stopcol, startcol = GetLeft(); 
  1670.     long newright= r.left;
  1671.  
  1672.     for (stopcol= startcol; stopcol<GetMaxCols() && newright<r.right; stopcol++) {
  1673.         //if (fWidths) newright += fWidths[startcol]; else 
  1674.         newright += GetItemWidth(0);
  1675.         // optimize later -- check update region by each char rect ...
  1676.         //if (Nlm_RectInRgn (&item_rect, Nlm_updateRgn)) done= true;
  1677.         }
  1678.  
  1679.     DSequence* aSeq= fSeqList->SeqAt(row);
  1680.     if (aSeq && aSeq->Bases()) {
  1681.         char* hSeq= aSeq->Bases();
  1682.         fCurSeq= aSeq;
  1683.         //Boolean dostyles= (fCurSeq && fCurSeq->MasksOk());
  1684.         
  1685.         long len= Min( aSeq->LengthF(), stopcol) - startcol;
  1686.         if (len>0) {
  1687.             Nlm_MoveTo( r.left, r.bottom-2); //bottom-5
  1688.             if (fColorBases == kBaseVarLite && fColcolors) 
  1689.                 gDrawGreyBases.doDraw(this,DBaseColors::gNAcolors, gSwapBackground, hSeq, startcol, len, row);
  1690.             else if (aSeq->Kind() == DSequence::kAmino)
  1691.                 DRAWALN(DBaseColors::gAAcolors, gSwapBackground, hSeq, startcol, len, row);
  1692.             else
  1693.                 DRAWALN(DBaseColors::gNAcolors, gSwapBackground, hSeq, startcol, len, row);
  1694.             }
  1695.         }
  1696.     fColsDrawn= stopcol - startcol;
  1697. }  
  1698.  
  1699.  
  1700. void DAlnView::DrawNoColors(Nlm_RecT r, long row)
  1701.     Nlm_RecT crec;
  1702.     long atx, aty;
  1703.     long stopcol, startcol = GetLeft(); 
  1704.     long newright= r.left;
  1705.     short    cwidth= GetItemWidth(0); // BaseCharWidth(); //fItemWidth
  1706.     
  1707.     for (stopcol= startcol; stopcol<GetMaxCols() && newright<r.right; stopcol++) {
  1708.         //if (fWidths) newright += fWidths[startcol]; else 
  1709.         newright += cwidth;
  1710.         // optimize later -- check update region by each char rect ...
  1711.         //if (Nlm_RectInRgn (&item_rect, Nlm_updateRgn)) done= true;
  1712.         }
  1713.  
  1714.     DSequence* aSeq= fSeqList->SeqAt(row);
  1715.     if (aSeq && aSeq->Bases()) {
  1716.         char *s, se, *hSeq= aSeq->Bases();
  1717.         long len= Min( aSeq->LengthF(), stopcol) - startcol;
  1718.         if (len>0) {
  1719. #if MASKS
  1720.             if (fMaskLevel>0) {
  1721.                 atx= r.left;
  1722.                 aty= r.bottom-2;
  1723.                 stopcol= startcol + len;
  1724.                 for (long i= startcol; i<stopcol; i++) {
  1725.                     //Boolean notInverted= true;
  1726.                     Nlm_MoveTo(atx, aty);
  1727.                     Nlm_PaintChar(hSeq[i]);
  1728.                     short maskval= aSeq->MaskAt(i, fMaskLevel);
  1729.                     if (maskval>0) {
  1730.                         Nlm_LoadRect( &crec, atx, r.top, atx+cwidth, r.bottom); 
  1731.                         //Nlm_FrameRect( &crec); //<< ? bad for Motif, also messy looking
  1732.                         DTableView::InvertRect( crec); 
  1733.                         //notInverted= false;
  1734.                         }
  1735. #if 0
  1736.     // no -- if fMaskLevel>0, don't do any selection inversion !
  1737.             if (notInverted) {
  1738.               if (this->fSelection->IsSelected(row, i)) {
  1739.                     Nlm_LoadRect( &crec, atx, r.top, atx+cwidth, r.bottom); 
  1740.                     this->DTableView::InvertRect( crec);
  1741.                     }
  1742.                 }
  1743. #endif
  1744.                     atx += cwidth;
  1745.                     }
  1746.                 }
  1747.             else 
  1748. #endif
  1749.                 {
  1750.                 Nlm_MoveTo( r.left, r.bottom-2); //bottom-5
  1751.                 s= hSeq+startcol;
  1752.                 se= s[len];
  1753.                 s[len]= 0;
  1754.                 Nlm_PaintString( s); 
  1755.                 s[len]= se;
  1756.                 //Nlm_DrawText( &r, hSeq+startcol, len, 'l', false); // gray== false
  1757. #if 1
  1758.                 atx= r.left;
  1759.                 for (long i= startcol; i<stopcol; i++) {
  1760.                   if (this->fSelection->IsSelected(row, i)) {
  1761.                         Nlm_LoadRect( &crec, atx, r.top, atx+cwidth, r.bottom); 
  1762.                         this->DTableView::InvertRect( crec);
  1763.                         }
  1764.                     atx += cwidth;
  1765.                     }
  1766. #endif
  1767.                 }
  1768.         }
  1769.         }
  1770.     fColsDrawn= stopcol - startcol;
  1771. }  
  1772.  
  1773.         
  1774. void DAlnView::Draw()
  1775. {
  1776.  
  1777.     if (fColorBases == kBaseVarLite) {
  1778.         // find grey color mask for base columns in view 
  1779.         Nlm_RecT r = fRect;
  1780.         long    row = 0; 
  1781.         long     ncols, icol, stopcol, startcol = GetLeft(); 
  1782.         long     newright= r.left;
  1783.         long    basecount['~'];
  1784.         char    maxc, ac;
  1785.         
  1786.         for (stopcol= startcol; stopcol < GetMaxCols() && newright<r.right; stopcol++) {
  1787.             newright += GetItemWidth(0);
  1788.             }
  1789.         ncols= stopcol - startcol + 1;
  1790.         
  1791.         if (ncols != fNcolcolors || !fColcolors) {
  1792.             MemFree( fColcolors);
  1793.             fColcolors= (unsigned long*) MemNew(ncols * sizeof(unsigned long));
  1794.             }
  1795.         fNcolcolors= ncols;
  1796.         
  1797.         for (icol= startcol; icol<stopcol; icol++) {
  1798.             long maxcount, nbases= 0;
  1799.             for (ac=0; ac<'~'; ac++) basecount[ac]= 0;
  1800.             for (row= 0; row < GetMaxRows(); row++) {
  1801.                 DSequence* aSeq= fSeqList->SeqAt(row);
  1802.                 if (aSeq && aSeq->Bases() && aSeq->LengthF()>icol) {
  1803.                   char ch= aSeq->Bases()[icol];
  1804.                   if (ch == DSequence::indelHard
  1805.                    || ch == DSequence::indelSoft
  1806.                    || ch == DSequence::indelEdge)
  1807.                        ;
  1808.                     else { 
  1809.                         ch |= 32;    // use case-insensitive ?
  1810.                         // ?? or use DSequence::NucleicBits(ch) ??
  1811.                         basecount[ch]++;
  1812.                     nbases++;
  1813.                     }
  1814.                     }
  1815.                 }
  1816.             for (maxcount=0, ac=0; ac<'~'; ac++) 
  1817.              if (basecount[ac] > maxcount) {
  1818.                 maxcount= basecount[ac];
  1819.                 maxc= ac;
  1820.                 }
  1821.                      
  1822.             enum { kMaxGrey = 127 }; // 255 is max color level
  1823.             unsigned long greycolor = 0;
  1824.             unsigned short level;
  1825.             float flevel= float(maxcount) / float(nbases);
  1826.             level= kMaxGrey - (kMaxGrey * flevel);
  1827.             level= 0xff & (level << 1);
  1828.  
  1829. #ifdef WIN_MAC
  1830.             greycolor= (((ulong)level)<<16) | (level<<8) | level;
  1831. #endif
  1832. #ifdef WIN_MSWIN
  1833.           greycolor = (level | level << 8 | ((ulong)level) << 16); //== RGB (cr, cg, cb);
  1834. #endif
  1835. #ifdef WIN_MOTIF
  1836.             // X uses mapped values thru setcolor -- need to do the select/get dance
  1837.             Nlm_SelectColor( level, level, level);
  1838.             greycolor= Nlm_GetColor();
  1839. #endif
  1840.             fColcolors[icol-startcol]= greycolor;
  1841.          
  1842.             }
  1843.         
  1844.         }
  1845.  
  1846.     DTableView::Draw();
  1847. }
  1848.  
  1849.  
  1850. void DAlnView::DrawRow(Nlm_RecT r, long row)
  1851. {
  1852.      // DTableView::Draw() does SelectFont(fFont)
  1853.   if (fEditRow == row) {
  1854.          // do nothing? , fEditSeq handle's redraw ??
  1855.         fColsDrawn= 0;
  1856.          }
  1857.      else switch (fColorBases) {
  1858.          case kBaseVarLite:
  1859.          case kBaseColor: 
  1860.              DrawAllColors(r, row); 
  1861.              break;
  1862.          case kBaseBlack: 
  1863.          default: 
  1864.              DrawNoColors(r, row); 
  1865.              break;
  1866.          }
  1867. }
  1868.  
  1869.         
  1870. void DAlnIndex::DrawCell(Nlm_RecT r, long row, long col)
  1871. {
  1872.     // add cols for Kind(), Length(), Origin(), UpdateTime(), Info()(wide col..better in box)
  1873.     char *cp, buf[128];
  1874.     buf[0]= 0;
  1875.     r.right -= 2; // inset
  1876.     
  1877.     DSequence* aSeq= fSeqList->SeqAt(row);
  1878.     if (aSeq) {
  1879. #ifdef TESTVARHEIGHT
  1880.          if (aSeq->ShowORF()) r.bottom -= kORFxtraHeight;
  1881. #endif         
  1882.         // from 0 .. kAlnIndexCols-1
  1883.         switch (col) {
  1884.         case kAlnIndexName: 
  1885.             if (aSeq->Changed())  Nlm_SelectFont(gUlineTextFont); //gBoldTextFont  
  1886.             cp= aSeq->Name();
  1887.             Nlm_DrawString( &r, cp, 'r', false);
  1888.             if (aSeq->Changed()) {     Nlm_SelectFont(fFont); }
  1889.             break;
  1890.         case kAlnIndexSize:
  1891.             sprintf(buf,"%d", aSeq->LengthF()); 
  1892.             Nlm_DrawString( &r, buf, 'r', false);
  1893.             break;
  1894.         case kAlnIndexKind:
  1895.             cp= aSeq->KindStr();
  1896.             Nlm_DrawString( &r, cp, 'r', false);
  1897.             break;
  1898.         case kAlnIndexRow:
  1899.             sprintf(buf,"%d", row+1);
  1900.             Nlm_DrawString( &r, buf, 'r', false);
  1901.             break;
  1902.         case kAlnIndexShowORF:
  1903. #if 0
  1904.             Nlm_MoveTo( r.left+1, r.bottom);
  1905.             if (aSeq->ShowORF()) Nlm_Medium();
  1906.             Nlm_PaintString( "Show");  
  1907.             Nlm_Solid();
  1908.             Nlm_PaintString( "/");  
  1909.             if (!aSeq->ShowORF()) Nlm_Medium();
  1910.             Nlm_PaintString( "Hide");  
  1911.             Nlm_Solid();
  1912.             Nlm_PaintString( " ORF");  
  1913. #else
  1914.             if (aSeq->ShowORF()) cp= "Hide ORF";
  1915.             else cp= "Show ORF";
  1916.             Nlm_DrawString( &r, cp, 'c', false); //true == grey
  1917. #endif
  1918.             break;
  1919.         case kAlnIndexChecksum:
  1920.             sprintf(buf,"%8lx", aSeq->Checksum());
  1921.             Nlm_DrawString( &r, buf, 'r', false);
  1922.             break;
  1923.             
  1924.             }
  1925.             
  1926. #ifdef notnow_TESTVARHEIGHT
  1927.         if (aSeq->ShowORF()) {
  1928.             r.top += kORFxtraHeight;
  1929.             r.bottom += kORFxtraHeight;
  1930.             Nlm_DrawString( &r, "orf", 'r', false);
  1931.             }
  1932. #endif         
  1933.         }
  1934. }
  1935.  
  1936.  
  1937. void DAlnITitle::DrawCell(Nlm_RecT r, long row, long col)
  1938. {
  1939.     r.right -= 2; // inset
  1940.     switch (col) {
  1941.         case kAlnIndexName: 
  1942.             Nlm_DrawString( &r, "Name", 'r', false);
  1943.             break;
  1944.         case kAlnIndexSize:
  1945.             Nlm_DrawString( &r, "Bases", 'r', false);
  1946.             break;
  1947.         case kAlnIndexKind:
  1948.             Nlm_DrawString( &r, "Kind", 'r', false);
  1949.             break;
  1950.         case kAlnIndexRow:
  1951.             Nlm_DrawString( &r, "Row", 'r', false);
  1952.             break;
  1953.         case kAlnIndexShowORF:
  1954.             Nlm_DrawString( &r, "ORF view", 'r', false);
  1955.             break;
  1956.         case kAlnIndexChecksum:
  1957.             Nlm_DrawString( &r, "Checksum", 'r', false);
  1958.             break;
  1959.         }
  1960. }
  1961.  
  1962. void DAlnHIndex::Draw()
  1963. {
  1964.     Nlm_RecT r;
  1965.     long atx, aty;
  1966.     char nums[128];
  1967.  
  1968.     fDoc->fAlnView->SelectFont();  
  1969.     ViewRect( r);
  1970.     long stopcol, startcol = fDoc->fAlnView->GetLeft(); 
  1971.     long newright= r.left;
  1972.     short    cwidth= gAlnCharWidth; //BaseCharWidth(); // fDoc->fAlnView->GetItemWidth(0);
  1973.  
  1974. #if 0    
  1975.     Nlm_RecT vr;
  1976.     // is ViewRect bad !? (too short)  YES - isn't grown !?
  1977.   fDoc->fAlnView->ViewRect( vr);
  1978.   r.right= vr.right; //??
  1979.     Nlm_ClipRect( &r); // ?? why is hindex clipped to small !?
  1980. #endif
  1981.     
  1982.     for (stopcol= startcol; 
  1983.         stopcol < fDoc->fAlnView->GetMaxCols() && newright<r.right;
  1984.       stopcol++) {
  1985.         newright += cwidth;
  1986.         }
  1987.  
  1988.     long len= stopcol - startcol;
  1989.     atx= r.left-(cwidth/2);
  1990.     aty= r.bottom;
  1991.     for (long i= startcol; i<=stopcol; i++) {
  1992.         if (i == 0) ; //skip
  1993.         else if (i % 10 == 0) {
  1994.             Nlm_MoveTo(atx, aty);
  1995.             Nlm_LineTo(atx, aty-2);
  1996.             Nlm_MoveTo(atx+1, aty);
  1997.             Nlm_LineTo(atx+1, aty-2);
  1998.             sprintf( nums, "%d", i);
  1999.             long ws= Nlm_StringWidth(nums);
  2000.             Nlm_MoveTo( atx-(ws/ 2), aty-3);
  2001.             Nlm_PaintString(nums);
  2002.             //Nlm_DrawString( &r1, nums, 'c', false);
  2003.             }    
  2004.         else if (i % 5 == 0) {
  2005.             Nlm_MoveTo(atx, aty);
  2006.             Nlm_LineTo(atx, aty-6);
  2007.             }
  2008.         else {
  2009.             Nlm_MoveTo(atx, aty);
  2010.             Nlm_LineTo(atx, aty-2);
  2011.             }
  2012.         atx += cwidth;
  2013.         }
  2014. }
  2015.  
  2016.  
  2017.  
  2018.  
  2019.         
  2020.  
  2021. #if FIX_LATER
  2022. Boolean DAlnView::ContainsClipType(long aType) // override 
  2023. {
  2024.     return (aType == kAlnScrapType);
  2025.     //!? also (aType == 'TEXT') can be pasted as seq... w/ SeqReadScrap...
  2026. }
  2027. #endif
  2028.  
  2029. #if FIX_LATER
  2030. void DAlnView::WriteToDeskScrap() // override 
  2031. {
  2032.     short format = kGenBank;
  2033.     //if (fDoc->fFormatPop) format= fDoc->fFormatPop->GetValue(); 
  2034.     fSeqList->ClearSelections();
  2035.     char* textScrap= fSeqList->doWriteHandle( format);  
  2036.     short err = gClipboardMgr->PutDeskScrapData('TEXT', textScrap);
  2037.     MemFree( textScrap);
  2038. }
  2039. #endif
  2040.  
  2041.  
  2042.  
  2043.  
  2044.  
  2045. char* DAlnIndex::GetItemTitle(short item, char* title, size_t maxsize)   
  2046. {
  2047.   if (fSeqList && item <= fSeqList->GetSize()) {
  2048.         DSequence* aSeq= fSeqList->SeqAt(item);
  2049.         if (aSeq) {
  2050.             if (!title) title= (char*) MemNew( maxsize);
  2051.             StrNCpy( title, aSeq->Name(), maxsize);
  2052.             return title;
  2053.             }
  2054.         }
  2055.     return NULL;
  2056. }
  2057.  
  2058.  
  2059.  
  2060.